package com.rpframework.website.device.act.api;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rpframework.core.BaseAct;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.BankUser;
import com.rpframework.website.device.domain.Cash;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.BankUserService;
import com.rpframework.website.device.service.CashService;
import com.rpframework.website.device.service.UserService;
/**
 * 互动 跳蚤市场是由app发的
 * 另外三个是后台发的
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("api/cash")
public class ApiCashAct extends BaseAct{
	@Resource CashService service;
	@Resource BankUserService bankUserService;
	@Resource UserService userService;

	@RequestMapping(value="/add",produces = "application/json; charset=utf-8")
	public @ResponseBody String add(
			@RequestParam(value = "bankuserId") Integer bankuserId,//我的银行卡列表，某张银行卡
			@RequestParam(value = "money") Double money,//金额
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		User user = userService.select(sessionuser.getId());
		JsonObject json = new JsonObject();
		BankUser bankuser = bankUserService.select(bankuserId);
		if(sessionuser.getBalance()<money){
			json.addProperty("code", 500);
			json.addProperty("msg", "余额不足");
			if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
			return ""+json ;
		}
		if(bankuser !=null){
			Cash t = new Cash();
			t.setBankName(bankuser.getName());
			t.setCardNo(bankuser.getCardNo());
			t.setMoney(money);
			t.setUserId(sessionuser.getId());
			t.setStatus(1);
			t.setCreateTime(System.currentTimeMillis()/1000);
			t.setState(1);
			t.setBalance(user.getBalance());//每次提现申请 钱都减掉了，但这里展示的是用户实际 余额
			boolean flag = service.insert(t);
			if(flag){
				json.addProperty("code", 200);
				json.addProperty("msg", "申请提现成功");
				//先把余额扣掉，如果人工审核 不通过 再退回 
				user.setBalance(user.getBalance() - money);
				userService.update(user);
			}else{
				json.addProperty("code", 500);
				json.addProperty("msg", "申请提现失败");
			}
		}else{
			json.addProperty("code", 500);
			json.addProperty("msg", "申请提现失败");
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}	
	
	@RequestMapping(value="/list",produces = "application/json; charset=utf-8")
	public @ResponseBody String list(@RequestParam(value="pager",required = false ) Pager<Cash> pager,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session
			){
		if(pager==null){
			pager=new Pager<Cash>();
		}
		User sessionUser = (User) session.getAttribute(SESSION_USER_KEY);
		pager.getSearchMap().put("userId", sessionUser.getId().toString());
		pager = service.getPager(pager);
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<Cash> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(Cash t : list){
				JsonObject obj = new JsonObject();
					obj.addProperty("id", t.getId());	
					obj.addProperty("status", t.getStatus());	
					obj.addProperty("money", t.getMoney());	
					obj.addProperty("balance",t.getBalance());	
					obj.addProperty("time", TagUtils.formatDate(t.getCreateTime()));	
					array.add(obj);
			}
			json.add("arrays", array);
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}	
}
